package com.hydl.power.utils;

import com.alibaba.fastjson.JSONObject;
import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.impl.SingleMessage;
import com.gexin.rp.sdk.base.impl.Target;
import com.gexin.rp.sdk.base.notify.Notify;
import com.gexin.rp.sdk.base.payload.APNPayload;
import com.gexin.rp.sdk.dto.GtReq;
import com.gexin.rp.sdk.http.IGtPush;
import com.gexin.rp.sdk.template.NotificationTemplate;
import com.gexin.rp.sdk.template.TransmissionTemplate;
import com.gexin.rp.sdk.template.style.Style0;
import com.gexin.rp.sdk.template.style.Style6;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class PushUtils {

    private static final String APP_ID = "Tqt0N4GOEC9MXZ7aSZ54rA";
    private static final String APP_KEY = "Thvat64vZj6Tw3eGkgJUvA";
    private static final String MASTER_SECRET = "kbe2eGb22G8GLTdHfhdZl7";
    private static final String URL = "http://api.getui.com/apiex.htm";

    /**
     *
     * @param clientIdList 手机clientID
     * @param powerRoomName 配电房名字
     * @param deviceName 设备名
     * @param fault 故障内容
     * @return IPushResult
     */
    public static IPushResult sendMessage(List<String> clientIdList,String powerRoomName, String deviceName, String fault) {
        if (clientIdList!=null && clientIdList.size()>0) {
            NotificationTemplate template = new NotificationTemplate();
            template.setAppId(APP_ID);
            template.setAppkey(APP_KEY);
            Notify notify = new Notify();
            notify.setTitle(powerRoomName + "告警");
            notify.setContent(deviceName + fault);
            notify.setIntent("intent:#Intent;launchFlags=0x04000000;action=android.intent.action.oppopush;package=com.hydl.client;component=com.hydl.client/io.dcloud.PandoraEntry;S.UP-OL-SU=true;end");
            Style0 style = new Style0();
            //style.setBadgeAddNum(1);
            style.setTitle(powerRoomName + "告警");
            style.setText(deviceName + fault);
            style.setRing(true);
            style.setVibrate(true);
            style.setClearable(true);
            style.setChannelLevel(3);

            template.setStyle(style);
            template.set3rdNotifyInfo(notify);
            template.setTransmissionType(1);
            IGtPush push = new IGtPush(URL, APP_KEY, MASTER_SECRET);
            SingleMessage message = new SingleMessage();
            message.setOffline(true);
            message.setOfflineExpireTime(24 * 3600 * 1000);
            message.setData(template);
            message.setPushNetWorkType(0);
            Target target = new Target();
            target.setAppId(APP_ID);
            IPushResult ret = null;
            for (String  clientId:clientIdList){
                target.setClientId(clientId);
                ret= push.pushMessageToSingle(message, target);
            }
            return ret;
        }
        return null;
    }



    public static void main(String[] args) {
       // String clientId="2dc0ad0972e72e15536dead5047d4487";
        for (int i=0;i<10; i++){
            String clientId="966b0760b56c05ca8c21faab88e8f04c";
            List<String> clientIdList=new ArrayList<>();
            clientIdList.add(clientId);
            IPushResult iPushResult = sendMessage(clientIdList, "配电房", "设备", "故障");
            System.out.println(JSONObject.toJSON(iPushResult));

        }

    }

}
